import { Request, RequestHandler, Response } from 'express';
import {
  IGetPermissionByIdsReq,
} from '../../models/permission.model';
import * as PermissionService from '../services/permission.service';

/**
 * Get active permission records
 *
 * @param req Express Request
 * @param res Express Response
 */
export const getPermissions: RequestHandler = async (req: Request, res: Response) => {
    try {
      const permissions = await PermissionService.getPermissions();
  
      res.status(200).json({
        data: permissions,
        code: 2000,
      })
    } catch (error) {
      console.error('[permissions.controller][getPermissions][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
      res.status(500).json({
        message: '权限列表获取失败！'
      });
    }
  };

/**
 * Get permission records based on id list provided
 *
 * @param req Express Request
 * @param res Express Response
 */
// @ts-ignore
export const getPermissionByIds: RequestHandler = async (req: IGetPermissionByIdsReq, res: Response) => {
  try {
    const idList = req.params.ids.toString().split(',').map((item) => parseInt(item));
    const permissions = await PermissionService.getPermissionsByIds({...idList});

    res.status(200).json({
      data: permissions,
      code: 2000,
    })
  } catch (error) {
    console.error('[permissions.controller][getPermissionById][Error] ', typeof error === 'object' ? JSON.stringify(error) : error);
    res.status(500).json({
      message: '获取权限信息失败！'
    });
  }
};